package it.ap.sa.acqua.fish.service;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import it.ap.sa.acqua.dao.beans.SearchFishFilter;
import it.ap.sa.acqua.dao.beans.TFishBean;
import it.ap.sa.acqua.dao.impl.TFishDao;
import it.ap.sa.acqua.dao.impl.TFishImageDao;
import it.ap.sa.acqua.dao.impl.TTankDao;
import it.ap.sa.acqua.dao.model.TFish;
import it.ap.sa.acqua.dao.model.TFishImage;
import it.ap.sa.acqua.dao.model.TTank;
import it.ap.sa.core.util.IBeanPopulate;
import it.ap.sa.core.utils.CoreProperties;
import it.ap.sa.core.utils.ListUtils;

/**
 * @author Alessandro Paolillo
 */
@Service
public class FishService implements IFishService {

	private static final Logger logger = LoggerFactory.getLogger(FishService.class.getName());

	@Autowired
	private IBeanPopulate<TFish, TFishBean> fishConverter;
	
	@Autowired
	private TFishDao tFishDao;
	
	@Autowired
	private TFishImageDao tFishImageDao;
	
	@Autowired
	private TTankDao tTankDao;
	
	@Autowired
	private CoreProperties properties;
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public TFishBean getFish(Long id) throws Exception {
		
		TFishBean result = new TFishBean();
		try {
			
			TFish entity = tFishDao.findByPk( id ); 
			if (entity!=null)
				result = this.fishConverter.convertdbToPage(entity);
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("getFish >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return result;
	}
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public List<TFishBean> getFishs( ) throws Exception {
		
		List<TFishBean> resultList = new ArrayList<TFishBean>();
		try {
			
			List<TFish> list = tFishDao.getFishs(  );
			if(ListUtils.listIsNotEmpty(list)) 
				for (TFish entity : list) {
					TFishBean bean = this.fishConverter.convertdbToPage(entity);
					TFishImage entityImage = tFishImageDao.findByIdFish( entity.getId() );
					if ( entityImage!=null )
						bean.setUrlImage( properties.FISH_IMAGE_URL+entityImage.getUrl()+properties.FISH_IMAGE_EXT );
					resultList.add( bean );
				}
				
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("getFishs >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return resultList;
	}
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public List<TFishBean> searchFish( SearchFishFilter searchFishFilter) throws Exception {
		
		List<TFishBean> resultList = new ArrayList<TFishBean>();
		try {
			
			List<TFish> list = tFishDao.searchFish( searchFishFilter );
			if(ListUtils.listIsNotEmpty(list)) 
				for (TFish entity : list) {
					TFishBean bean = this.fishConverter.convertdbToPage(entity);
					TFishImage entityImage = tFishImageDao.findByIdFish( entity.getId() );
					if ( entityImage!=null )
						bean.setUrlImage( properties.FISH_IMAGE_URL+entityImage.getUrl()+properties.FISH_IMAGE_EXT );
					resultList.add( bean );
				}
				
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("searchFish >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return resultList;
	}
	
	@Override
	@Transactional(readOnly=false,rollbackFor=Exception.class)
	public Long searchCountFish( SearchFishFilter searchFishFilter) throws Exception {
		
		Long searchCountFishValue = new Long(0);
		try {
			searchCountFishValue = tFishDao.searchCountFish( searchFishFilter );
				
			logger.info("Query eseguita correttamente");
		} catch(Exception e){
			logger.error("searchCountFish >>>>>>>>>>>> " + e, e);
			throw(e);
		}
		return searchCountFishValue;
	}
}
